<!--
 * @Description: 工单列表
 * @Author: Ronda
 * @Date: 2022-02-16 09:30:08
 * @LastEditors: 石洪旭 2088649550@qq.com
 * @LastEditTime: 2025-09-28 10:13:22
-->
<template>
  <div>
    <!-- 输入框和录入按钮开始 -->
    <div class="btns">

      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-row>
          <el-col :span="18">
            <el-form-item>
          <el-select clearable v-model="form.status" placeholder="请选择设备状态">
            <el-option label="待接单" value="待接单"></el-option>
            <el-option label="已完成" value="已完成"></el-option>
            <el-option label="进行中" value="进行中"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select clearable v-model="form.type" placeholder="请选择工单类型">
            <el-option label="维修" value="维修"></el-option>
            <el-option label="拆机" value="拆机"></el-option>
            <el-option label="安装" value="安装"></el-option>

          </el-select>
        </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="toSearch">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
    </div>
    <!-- 输入框和录入按钮结束 -->
    <!-- 表格开始 -->
    <div class="table">
      <el-table :data="engData" style="width: 100%">
        <el-table-column type="index" :index="1" label="序号" width="48" align="center">
        </el-table-column>
        <el-table-column prop="engineer_name" label="工程名称" width="553" align="center">
        </el-table-column>
        <el-table-column prop="device_name" label="设备名称" width="200" align="center">
        </el-table-column>
        <el-table-column prop="bill_why" label="问题描述" width="200" align="center">
        </el-table-column>
        <el-table-column prop="status" label="状态" width="220" align="center">
          <template v-slot="scope">
            <div>
              <el-tag v-if="scope.row.status == '已完成'" effect="light" type="success">已完成</el-tag>
              <el-tag v-else-if="scope.row.status == '进行中'" effect="light" type="warning">进行中</el-tag>
              <el-tag v-else effect="light" type="danger">待接单</el-tag>
            </div>
          </template>
        </el-table-column>
        <template>
          <div>
            <el-table-column prop="type" label="工单类型" width="100" align="center">
            </el-table-column>
            <el-table-column label="操作" width="100" align="center">
              <template v-slot="scope">
                <el-link @click="toBind(scope.row)" type="success">详情</el-link>
                <el-link :underline="false" type="success">取消</el-link>
              </template>
            </el-table-column>
          </div>
        </template>


      </el-table>
    </div>
    <!-- 表格结束 -->
    <!-- 分页开始 -->
    <div class="page" style="margin-top: 10px;">
      <el-pagination hide-on-single-page @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="params.page" :page-sizes="[2, 8]" :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <!-- 分页结束 -->
  </div>
</template>

<script>
import { get, del, post } from '@/utils/request'
import moment from 'moment'
// 引入省市区数据
import pca from '@/assets/pca.json'
// 引入lodash工具库 
import _ from 'lodash';
export default {
  data() {
    return {
      pca,
      // 表单数据对象 
      form: {},
      // 表格显示的数据 
      engData: [],
      params: {
        page: 1,
        pageSize: 2
      },
      total: 0


    }
  },
  methods: {
    toSearch() {
      this.params.page = 1;
      this.getEngData();
    },
    // 去绑定 
    toBind(row) {
      // 跳转到工程详情页面 编程式路由导航 
      this.$router.push({
        path: '/order/Details',
        query: row
      })
    },
    toSubmit() {
      this.$refs['myForm'].validate(async (valid) => {
        if (valid) {
          // 表单校验通过  发起保存或者更新请求 
          // alert('submit!');
          // console.log(this.formSave);
          let data = _.clone(this.formSave);
          data.address = data.address1.join(' ') + ' ' + data.address;
          delete data.address1;
          console.log(data, '-------------');
          let res = await post('/workorder/findById', data);
          if (res.status == 200) {
            this.$notify({
              title: "成功",
              message: res.message,
              type: 'success'
            });
            this.visible = false;
            // 重新查询数据
            this.getEngData();
          } else {
            return false;
          }
        } else {
          // 表单校验失败
          console.log('error submit!!');
          return false;
        }
      });
    },
    toEdit(row) {
      this.visible = true;
      this.title = '修改工程信息';
      // ...拓展运算符  展开当前行对象
      // this.formSave = {...row};
      this.formSave = _.clone(row);
      this.formSave.address1 = this.formSave.address.split(' ').slice(0, 3);
      this.formSave.address = this.formSave.address.split(' ').slice(3).join(' ');
    },
    moment,
    // 每页条数发生改变就会触发 
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`); //val 2条/页--->5条/页 val-->5
      this.params.pageSize = val;
      this.getEngData();
    },
    // 页码发生改变就会触发 
    handleCurrentChange(val) {
      // val表示就是页码 1-->3 
      // console.log(`当前页: ${val}`); 1 2 3
      this.params.page = val;
      // 重新查询数据 
      this.getEngData();
    },
    // 获取工程数据方法 


    async getEngData() {
      let temp = {
        ...this.form,
        ...this.params
      };

      let res = await get('/workorder/pageQuery', temp);
      console.log(res, '获取响应');
      this.engData = res.data.list;
      this.total = res.data.total;
    }
  },
  // 最早的异步请求 
  created() {
    this.getEngData();
  }
}
</script>
<style lang="scss" scoped>
// vue2深度选择器   
::v-deep .el-link--inner {
  margin-left: 5px;
}

.input {
  width: 280px;
}

.select {
  width: 280px;
}

.demo-drawer__footer {
  border-top: 1px solid #ececec;
  padding-top: 10px;
  position: fixed;
  bottom: 50px;
  width: 50%;
  text-align: center;
}
</style>